import json
import logging
import requests


def chat_geek_info():
    # 从文件中读取数据
    with open('data/chat_geek.json', 'r',encoding='utf-8') as f:
        response_data = json.load(f)
    # 获取字段值
    geek_base_info = response_data["zpData"]["data"]
    name = geek_base_info.get('name')
    gender = geek_base_info.get('gender')
    positionStatus = geek_base_info.get('positionStatus')
    ageDesc = geek_base_info.get('ageDesc')
    position = geek_base_info.get('position')
    toPosition = geek_base_info.get('toPosition')
    phone = geek_base_info.get('phone')
    weixin = geek_base_info.get('weixin')


    # 判断手机号和微信号是否都为 None，如果都为 None，则不推送
    if phone is None and weixin is None:
        logging.warning("手机号和微信号都为 None，未发送消息")
        return 0 # 如果手机号和微信号都为 None，则直接返回，不发送消息


    # Webhook URL
    webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/1af951b9-a53a-4f08-bbfb-67226bca90d5"

    # 拼接消息内容
    message = {
        "msg_type": "text",
        "content": {
            "text": (
                f"姓名：{name}\n"
                f"手机号：{phone}\n"
                f"性别：{'女' if gender == 0 else '男'}\n"
                f"年龄：{ageDesc}\n"
                f"期望职位（岗位）：{toPosition}\n"
                f"职位名称：{position}\n"
                f"求职状态（在职-离职）：{positionStatus}\n"
                f"微信：{weixin}\n"
            )
        }
    }
    # 打印结果
    print(json.dumps(message, ensure_ascii=False, indent=4))

    # 设置请求头
    headers = {
        "Content-Type": "application/json"
    }

    # 发送 POST 请求
    try:
        response = requests.post(webhook_url, headers=headers, data=json.dumps(message))
        response.raise_for_status()  # 检查请求是否成功
        logging.info("消息发送成功")
        logging.info("响应内容: %s", response.json())
        return 1
    except requests.exceptions.RequestException as e:
        logging.error("发送消息失败: %s", e)

    return 0

